//
//  ViewController.m
//  LockViewDemo
//
//  Created by jianyong.wang on 2017/12/4.
//  Copyright © 2017年 jianyong.wang. All rights reserved.
//

#import "ViewController.h"
#import "LockView.h"
@interface ViewController ()<LockDelegate>
@property (weak, nonatomic) IBOutlet UIButton *resetBtn;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;
@property (weak, nonatomic) IBOutlet UIButton *testBtn;
@property (weak, nonatomic) IBOutlet UILabel *pwdLabell;
@property (weak, nonatomic) IBOutlet LockView *lockView;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;

@property (nonatomic,assign) BOOL isTest;//是否在测试
@property (nonatomic,strong) NSString * pwdString;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.lockView addPanGesture];//添加手势
    self.lockView.delegate = self;
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - delegate
-(void)lockView:(LockView *)lockView drawFinished:(NSMutableString *)pwd
{
    if (self.isTest) {
        [self.lockView checkResultPwd:[self valateResultPwd:pwd]];
        
    }else{
        self.pwdLabell.text = self.pwdString = pwd.length?pwd:@"--------";
        [self.lockView checkSetPwd:[self valateSetPwd:pwd]];
    }
    [self shakeAnimationForView:self.statusLabel];
}

/**
 判断输入的密码是否正确

 @param pwd 当前绘制的密码
 @return 密码状态
 */
-(ResultPwdType )valateResultPwd:(NSString *)pwd
{
    
    if (pwd.length < 4) {
        self.statusLabel.text = @"测试密码失败";
        return ResultPwdTypeNoEnough;
    }else if([pwd isEqualToString:self.pwdString]){
        self.statusLabel.text = @"测试密码成功";
        return ResultPwdTypeTrue;
    }
    self.statusLabel.text = @"测试密码失败";
    return ResultPwdTypeFalse;
}

/**
 设置密码的正确与否

 @param pwd 当前设置的密码
 @return 设置的密码状态
 */
-(SetPwdType )valateSetPwd:(NSString *)pwd
{
    self.statusLabel.text = pwd.length<4?@"设置密码失败":@"设置密码成功";
    if (pwd.length < 4) {
        return SetPwdTypeNoEnough;
    }else return SetPwdTypeTrue;
}
#pragma mark 抖动动画

/**
 抖动动画

 @param view 需要抖动的动画
 */
- (void)shakeAnimationForView:(UIView *)view {
    
    CALayer *viewLayer = view.layer;
    CGPoint position = viewLayer.position;
    CGPoint left = CGPointMake(position.x - 10, position.y);
    CGPoint right = CGPointMake(position.x + 10, position.y);
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [animation setFromValue:[NSValue valueWithCGPoint:left]];
    [animation setToValue:[NSValue valueWithCGPoint:right]];
    [animation setAutoreverses:YES]; // 平滑结束
    [animation setDuration:0.08];
    [animation setRepeatCount:3];
    
    [viewLayer addAnimation:animation forKey:nil];
}
#pragma mark - action

/**
 设置密码按钮点击事件

 @param sender <#sender description#>
 */
- (IBAction)resetPwd:(UIButton *)sender {
    if (!sender.selected) {
        sender.selected = true;
        self.testBtn.selected = false;
    }else return;
    self.saveBtn.hidden = false;
    self.lockView.isTest = self.isTest = false;
    [self.lockView clearLockView];
    self.statusLabel.text = @"设置密码中";
}

/**
 测试密码按钮点击事件

 @param sender <#sender description#>
 */
- (IBAction)testPwd:(UIButton *)sender {
    if (!sender.selected) {
        sender.selected = true;
        self.resetBtn.selected = false;
    }else return;
    self.saveBtn.hidden = true;
    self.lockView.isTest = self.isTest = true;
    [self.lockView clearLockView];
    self.statusLabel.text = @"测试密码中";
}



@end
